<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/23
 * Time: 14:54
 */
class Domain_Index{
  /**
   * 是否在指定商家下单
   */
  public function appointShop($userid){
    //获取所有购买的可抽奖的商家
    $Domain_Shop=new Domain_Shop();
    $allShop= $Domain_Shop->getShopId();
    if(!$allShop)return false;
    //获取今天该用户购买的次数(上面的指定商家,即可抽奖次数)
    $Domain_ShopUserBuy=new Domain_ShopUserBuy();
    $buyCounts=$Domain_ShopUserBuy->getTodayOrders($userid,$allShop);
    $buyCount=$buyCounts['count'];
    if(!$buyCount)return false;//没购买，返回false
    //今天的抽奖次数
    $Domain_Prized=new Domain_Prized();
    $prizedCounts=$Domain_Prized->getPrizedTimes($userid);
    $prizedCount=$prizedCounts['count'];
    if($prizedCount>=$buyCount)return false;//抽奖次数大于等于购买次数，返回false
    return true;
  }

  /**
   * 查看今天是否有奖
   */
  public function getTodayGift($userid){
    $Domain_Prized=new Domain_Prized();
    return $Domain_Prized->getTodayGift($userid);
  }

  /**
   * 抽奖概率
   */
  public function IsPrized($prt){
    $ok = mt_rand(1,100);
    if($ok<=$prt)//小于中奖概率
      return true;//中奖
    return false;//没中奖
  }

  /**
   * 获取所有奖项
   */
  public function getPrize(){
    $Domain_Prize=new Domain_Prize();
    return $Domain_Prize->getPrize();
  }

  /**
   *插入中奖纪录
   */
  public function InsertPrizedRecord($userid,$prizeId){
    $Domain_PrizeRecord=new  Domain_PrizeRecord();
    $Domain_PrizeRecord->InsertPrizedRecord($userid,$prizeId);
  }

  /**
   * 插入抽奖纪录
   */
  public function InsertPrized($userid,$isPrized){
    $Domain_Prized=new Domain_Prized();
    $Domain_Prized->InsertPrized($userid,$isPrized);
  }

  /**
   * 查看今天抽奖次数
   */
  public function getTodayPrizeTime($userid){
    $Domain_Prized=new Domain_Prized();
    $prizedTimes= $Domain_Prized->getPrizedTimes($userid);
    return $prizedTimes['count'];
  }

  /**
     * 用户当天抽奖
     */
  public function getGift($phone){
    $_SESSION['phone'] = $phone;
    if($this->getTodayPrizeTime($phone)>0)
      return array('code'=>-5,'prizeid'=>-1);//已下过单，不能抽

    //if(!$this->appointShop($phone))
    //  return array('code'=>-4,'prizeid'=>-1);//请在指定店铺下单
    //if($this->getTodayGift($phone))
    //{
    //  $this->InsertPrized($phone,false);//抽奖纪录
    //  return array('code'=>0,'prizeid'=>-1);//没有中奖(已经中过奖了)
    //}
    $allPrize=$this->getPrize();//所有的奖品
    $prizeProbability=$this->getProbability($allPrize);//中奖几率
    if(!$this->IsPrized($prizeProbability))//中奖几率
    {
      $this->InsertPrized($phone,false);//抽奖纪录
      return array('code'=>0,'prizeid'=>-1);//没有中奖
    }
    $this->InsertPrized($phone,true);//抽奖纪录
    //抽中奖中奖中奖中奖中奖中奖中奖中奖中奖中奖中奖中奖中奖中奖中奖中奖中奖中奖中奖中奖
    $prize=$this->prizeProbability($allPrize,$prizeProbability);
    if(!$prize)
      return array('code'=>999999,'prizeid'=>-1);//异常
    $Domain_PrizeRecord=new Domain_PrizeRecord();
    $todayPrizedCount= $Domain_PrizeRecord->getTodayPrize($prize['id']);
    if($prize['balance']<=$todayPrizedCount['count'])//奖品剩余量
      return array('code'=>-2,'prizeid'=>$prize['id']);//奖池为空
    $this->InsertPrizedRecord($phone,$prize['id']);//插入中奖纪录
    if($prize['type']==4){//优惠券
      $model = new Common_Function();
      $model ->setFanJuan($prize['code']);//向用户发送  优惠券
      return array('code'=>1,'prizeid'=>$prize['id']);
    }
    if($prize['type']==3){//三等奖
      return array('code'=>3,'prizeid'=>$prize['id']);
    }
    if($prize['type']==1||$prize['type']==2){//一二等奖
      return array('code'=>4,'prizeid'=>$prize['id']);
    }
    return array('code'=>999999,'prizeid'=>-1);//异常

  }

  /**
   * 抽奖概率筛选
   */
  private function getProbability($prizes){
    $sumProbability=0;//总中奖机会
    array_walk($prizes,function ($value,$key) use (&$sumProbability){
      $sumProbability+=$value['probability'];
    });
   return $sumProbability;
  }
  /**
   * 抽的什么奖
   */
  private function prizeProbability($prizes,$probability){
    $prizeKey= mt_rand(1,$probability);
    $prize=null;//暂时设定为0

    foreach($prizes as $value){
      if($prizeKey<=$value['probability']){
        $prize=$value;
        break;
      }
      $prizeKey-=$value['probability'];//减去相应概率
    }
    return $prize;
  }
}